/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ResourceRequirementComparisonForm.java
 *
 * Created on Aug 21, 2012, 3:16:47 PM
 */
package org.rooster.test;

import java.awt.Color;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author Savithri
 */
public class ResourceRequirementComparisonForm extends javax.swing.JFrame {

    /** Creates new form ResourceRequirementComparisonForm */
    public ResourceRequirementComparisonForm() throws SQLException {
        initComponents();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        chartPanel = new javax.swing.JPanel();
        employeeType = new javax.swing.JComboBox();
        section = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        chartPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Requirement Comparison Graph"));

        javax.swing.GroupLayout chartPanelLayout = new javax.swing.GroupLayout(chartPanel);
        chartPanel.setLayout(chartPanelLayout);
        chartPanelLayout.setHorizontalGroup(
            chartPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 437, Short.MAX_VALUE)
        );
        chartPanelLayout.setVerticalGroup(
            chartPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 323, Short.MAX_VALUE)
        );

        employeeType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "VOLUME", "NON_VOLUME" }));

        section.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Section1", "Section2", "Section 3", "Section 4", "Section5" }));
        section.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sectionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(employeeType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(section, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(chartPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(163, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(employeeType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(section, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(chartPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void sectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sectionActionPerformed
        int type = employeeType.getSelectedIndex();

        int index = section.getSelectedIndex();
        try {
            drawchart(type, index);
        } catch (SQLException ex) {
            Logger.getLogger(ResourceRequirementComparisonForm.class.getName()).log(Level.SEVERE, null, ex);
        }
        validate();
    }//GEN-LAST:event_sectionActionPerformed

    /**
     * @param args the command line arguments
     */
    public Vector getVolumeEmployeeDataSet() throws SQLException {

        ResourceRequirementComparison resourceCom = new ResourceRequirementComparison();

        Vector<Vector<Vector>> fullfilledReq = resourceCom.getFulfilledRequirement();
        Vector<Vector<Vector>> requirement = resourceCom.getRequirement();
        Vector<DefaultCategoryDataset> dataSets = new Vector<DefaultCategoryDataset>();

        for (int i = 0; i < fullfilledReq.size(); i++) {
            DefaultCategoryDataset dataset = new DefaultCategoryDataset();
            Vector<Vector> sectionFulfilled = fullfilledReq.get(i);
            Vector<Vector> sectiinNeed = requirement.get(i);

            for (int j = 0; j < sectiinNeed.size(); j++) {
                Vector timeSlotfulfilled = sectionFulfilled.get(j);
                Vector timeslotNeed = sectiinNeed.get(j);


                int volfulfilled = Integer.parseInt(timeSlotfulfilled.get(0).toString());
                int volneed = Integer.parseInt(timeslotNeed.get(0).toString());
                dataset.setValue(volneed, "Volume Required ", "" + j + "");
                dataset.setValue(volfulfilled, "Volume Fulfilled", "" + j + "");


            }
            dataSets.add(dataset);
        }


        return dataSets;

    }

    public Vector getNonVolumeEmployeeDataSet() throws SQLException {

        ResourceRequirementComparison resourceCom = new ResourceRequirementComparison();

        Vector<Vector<Vector>> fullfilledReq = resourceCom.getFulfilledRequirement();
        Vector<Vector<Vector>> requirement = resourceCom.getRequirement();
        Vector<DefaultCategoryDataset> dataSets = new Vector<DefaultCategoryDataset>();

        for (int i = 0; i < fullfilledReq.size(); i++) {
            DefaultCategoryDataset dataset = new DefaultCategoryDataset();
            Vector<Vector> sectionFulfilled = fullfilledReq.get(i);
            Vector<Vector> sectiinNeed = requirement.get(i);

            for (int j = 0; j < sectiinNeed.size(); j++) {
                Vector timeSlotfulfilled = sectionFulfilled.get(j);
                Vector timeslotNeed = sectiinNeed.get(j);


                int nonVolfulfilled = Integer.parseInt(timeSlotfulfilled.get(1).toString());
                int nonVolneed = Integer.parseInt(timeslotNeed.get(1).toString());
                dataset.setValue(nonVolneed, "NonVolume Required ", "" + j + "");
                dataset.setValue(nonVolfulfilled, "NonVolume Fulfilled", "" + j + "");


            }
            dataSets.add(dataset);
        }


        return dataSets;

    }

    public void drawchart(int type, int index) throws SQLException {
        Vector<DefaultCategoryDataset> dataSets;
        if (type == 0) {
            dataSets = getVolumeEmployeeDataSet();
        } else {
            dataSets = getNonVolumeEmployeeDataSet();

        }
        DefaultCategoryDataset data = new DefaultCategoryDataset();
        data = dataSets.get(index);



        JFreeChart chart = ChartFactory.createBarChart("" + data.getValue(0, 0) + "", "Time Slots", "Resource Count", data,
                PlotOrientation.VERTICAL, false, true, false);
        chartPanel.removeAll();
        ChartPanel myChart = new ChartPanel(chart);
        CategoryPlot plot = chart.getCategoryPlot();
        chart.setBackgroundPaint(Color.BLACK);
        plot.setBackgroundPaint(Color.BLACK);
        JLabel volume = new javax.swing.JLabel();
        volume.setText("Volume");
        volume.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/green.png")));
        JLabel nonVolume = new javax.swing.JLabel();
        nonVolume.setText("Non Volume");
        nonVolume.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/yellow.png")));
        JLabel need = new javax.swing.JLabel();
        need.setText("Need");
        need.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/blue.gif")));
          myChart.add(volume);
           myChart.add(nonVolume);
             myChart.add(need);
       
        BarRenderer r = (BarRenderer) chart.getCategoryPlot().getRenderer();
        //GradientPaint gradientpaint0 = new GradientPaint(0.0F, 0.0F, new Color(Integer.parseInt("7DEABE", 16)), 0.0F, 0.0F, new Color(Integer.parseInt("0000FF", 16)));
        Color volColor = new Color(0, 128, 0);
        Color nonVolColor = new Color(255, 252, 23);
        if (type == 0) {
            r.setSeriesPaint(0, volColor);
        } else {
            r.setSeriesPaint(0, nonVolColor);
        }

        r.setSeriesPaint(1, new Color(Integer.parseInt("306EFF", 16)));

        myChart.setBounds(10, 20, chartPanel.getWidth(), chartPanel.getHeight() - 10);
        chartPanel.add(myChart);
    
        chartPanel.validate();
        chartPanel.repaint();

    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    try {
                        UIManager.setLookAndFeel("com.jtattoo.plaf.hifi.HiFiLookAndFeel");
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(ResourceRequirementComparisonForm.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InstantiationException ex) {
                        Logger.getLogger(ResourceRequirementComparisonForm.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IllegalAccessException ex) {
                        Logger.getLogger(ResourceRequirementComparisonForm.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (UnsupportedLookAndFeelException ex) {
                        Logger.getLogger(ResourceRequirementComparisonForm.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    new ResourceRequirementComparisonForm().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(ResourceRequirementComparisonForm.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel chartPanel;
    private javax.swing.JComboBox employeeType;
    private javax.swing.JComboBox section;
    // End of variables declaration//GEN-END:variables
}
